package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.common.util.SecurityUtils;
import com.cdck.safe.dete.model.entity.ActivityUser;
import com.cdck.safe.dete.model.form.ActivityUserForm;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.security.userdetails.SysUserDetails;
import com.cdck.safe.dete.service.ActivityUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * (ActivityUser)表控制层
 *
 * @author wp
 * @since 2024-02-26 22:59:21
 */
@Api(value = "activityUser")
@RestController
@RequestMapping("activityUser")
@RequiredArgsConstructor
public class ActivityUserController {

    private final ActivityUserService activityUserService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams"  ,value ="查询条件" )
    })
    public PageResult<ActivityUser> queryByPage(ActivityUserForm queryParams) {
        IPage<ActivityUser> result = activityUserService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{activityId}/queryParticipate")
    public Result<ActivityUser> queryIsParticipate(
           @PathVariable Long activityId) {
        Long userId = SecurityUtils.getUserId();
        ActivityUser formData = activityUserService.queryIsParticipate(activityId, userId);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param activityId 活动id
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @GetMapping("/{activityId}/add")
    public Result add(
            @PathVariable Long activityId
        ) {
        Long userId = SecurityUtils.getUserId();
        ActivityUser activityUser = new ActivityUser();
        activityUser.setActivityId(String.valueOf(activityId));
        activityUser.setUserId(String.valueOf(userId));
        boolean result = this.activityUserService.saveActivityUser(activityUser);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateActivityUser(
            @PathVariable Long id,
            @RequestBody @Validated ActivityUser activityUser) {
        boolean result = activityUserService.updateActivityUser(id, activityUser);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteActivityUsers(
             @PathVariable String ids
    ) {
        boolean result = activityUserService.deleteActivityUsers(ids);
        return Result.judge(result);
    }

}

